﻿using ProxyTool.Services.Utils;
using RestSharp;
using RestSharp.Deserializers;
using RestSharp.Serializers;

namespace ProxyTool.Services
{
    /// <summary>
    ///     新的Json序列化
    /// </summary>
    public class NewSimpleJsonSerializer : ISerializer, IDeserializer
    {
        public NewSimpleJsonSerializer()
        {
            this.ContentType = "application/json; charset=utf-8";
        }

        /// <summary>
        ///     默认
        /// </summary>
        public static NewSimpleJsonSerializer Default
        {
            get { return new NewSimpleJsonSerializer(); }
        }

        /// <summary>
        ///     反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="response"></param>
        /// <returns></returns>
        public T Deserialize<T>(IRestResponse response)
        {
            return JsonHelper.ObjectFromJsonString<T>(response.Content);
        }

        public string DateFormat { get; set; }
        public string RootElement { get; set; }
        public string Namespace { get; set; }
        public string ContentType { get; set; }

        /// <summary>
        ///     序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string Serialize(object obj)
        {
            return JsonHelper.ObjectToJsonString(obj);
        }
    }
}